<?php

class McCube_Html_Listing extends McCube_Html_Abstact{

    /** */
    public static function get($name='listing'){
        return new self($name);
    }

    protected $_name         = '';
    protected $_theme        = 'classic';
    protected $_columns      = array();
    protected $_items        = array();
    protected $_itemsActions = array();
    protected $_actions      = array();

    /** */
    public function __construct($name='listing'){
        $this->_name = $name;
    }

    /** */
    public function __toString(){
        return $this->render();
    }

    /** */
    public function setColumn($name, $config){
        $this->_columns[$name] = $config;//new McCube_Html_Listing_Column($name, $config);
        return $this;
    }

    /** */
    public function setColumns($columns){
        foreach($columns as $name=>$config){
            $this->setColumn($name, $config);
        }
        return $this;
    }

    /** */
    public function setItems($items, $primaryKey='id'){
        $this->_items      = $items;
        $this->_primaryKey = $primaryKey;
        return $this;
    }

    /** */
    public function setItemActions($itemsActions){
        $this->_itemsActions = $itemsActions;
        return $this;
    }

    /** */
    public function setActions($actions){
        $this->_actions = $actions;
        return $this;
    }

    /** */
    public function render(){
        return '<div class="listing" id="'.$this->_name.'"></div>'
        .'<script type="text/javascript">$(document).ready(function() {'
        .'$.mcCube.listing.instance("'.$this->_name.'",'.json_encode(array(
                'theme'   => $this->_theme,
                'cssClass'=> 'listing',
                'actions' => $this->_actions
            )).')'
            .'.reset()'
            .'.setColumns('.json_encode($this->_columns).')'
            .'.setItems('
                .json_encode($this->_items)
                .','
                .json_encode(array(
                    'primaryKey' => $this->_primaryKey,
                    'actions'    => $this->_itemsActions
                ))
            .')'
            .'.render()'
        .';'
        .'});</script>';
    }


}